package cn.luyou.admin.controller;

import cn.luyou.admin.common.Result;
import cn.luyou.admin.entity.Question;
import cn.luyou.admin.service.QuestionService;
import cn.luyou.admin.utils.BaseContext;
import com.baomidou.mybatisplus.core.metadata.IPage;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ContentDisposition;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;

@RestController
@RequestMapping("/questions")
public class QuestionController {

    @Autowired
    private QuestionService questionService;

    @GetMapping
    public Result<IPage<Question>> getQuestions(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String subject,
            @RequestParam(required = false) String type,
            @RequestParam(required = false) String difficulty) {
        return Result.success(questionService.getQuestions(pageNum, pageSize, keyword, subject, type, difficulty));
    }

    @PostMapping
    public Result<Question> addQuestion(@RequestBody @Valid Question question, HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        question.setCreatedBy(BaseContext.getCurrentId());
        return Result.success(questionService.addQuestion(question));
    }

    @PutMapping("/{id}")
    public Result<Void> updateQuestion(@PathVariable Long id, @RequestBody @Valid Question question) {
        question.setId(id);
        questionService.updateQuestion(question);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result<Void> deleteQuestion(@PathVariable Long id) {
        questionService.deleteQuestion(id);
        return Result.success();
    }

    @PostMapping("/import")
    public Result<Void> importQuestions(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        questionService.importQuestions(file, BaseContext.getCurrentId());
        return Result.success();
    }

    @PostMapping("/export")
    public ResponseEntity<byte[]> exportQuestions(@RequestBody(required = false) List<Long> ids) throws IOException {
        byte[] excelContent = questionService.exportQuestions(ids);
        
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        headers.setContentDisposition(ContentDisposition.builder("attachment")
                .filename("questions.xlsx", StandardCharsets.UTF_8)
                .build());
        
        return new ResponseEntity<>(excelContent, headers, HttpStatus.OK);
    }

    @PostMapping("/remove-duplicates")
    public Result<Void> removeDuplicates() {
        questionService.removeDuplicates();
        return Result.success();
    }
} 